//
//  WJHomeViewCell.swift
//  01MicroBlog
//
//  Created by Apple on 15/8/3.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

let width = UIScreen.mainScreen().bounds.size.width

/// 微博 Cell 的标示符枚举
enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    /// 静态函数
    /// 根据微博数据，返回对应的标示符
    static func cellId(status: Status) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusCell: UITableViewCell {

    var status:Status? {
        didSet{
            topView.status = status
            pictureView?.status = status
            contentLabel.text = status?.text ?? ""
            
            // 设置配图视图的尺寸
            pictureHeightCons?.constant = pictureView!.bounds.size.height
            pictureWidthCons?.constant = pictureView!.bounds.size.width
            // 设置图片的顶部约束，如果图片没有高度，则顶部约束为0
            pictureTopCons!.constant = (pictureView!.bounds.size.height == 0) ?  0 : margin
        }
    }
    
     // 计算行高
    func getRowHeight(status:Status) -> CGFloat {
        
        self.status = status
        
        // 强行更新布局
        layoutIfNeeded()
        let h = CGRectGetMaxY(bottomView.frame)
        return h
    }
    
    /// 图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    /// 图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    // 图片的顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
   override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
        setUpUI()
    }
    
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
     func setUpUI() {
        
        // 加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView!)
        contentView.addSubview(bottomView)
        
//        topView.backgroundColor = UIColor.yellowColor()
        pictureView?.backgroundColor = UIColor.grayColor()
        
        // 设置约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 53))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: margin, y: margin))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -margin*2))
        
        // 先定义一个给定的图片约束，后续再修改
//        let cons = pictureView!.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: margin))
        
        // 设置图片宽高的约束，保证图片可见
//        pictureWidthCons = pictureView!.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureHeightCons = pictureView!.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureTopCons = pictureView?.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView!, size: CGSize(width: width, height: 44), offset: CGPoint(x: -margin, y: margin))
        

    }
    
    // 顶部视图
    private lazy var topView:StatusTopView = StatusTopView()
    // 内容文字
    lazy var contentLabel:UILabel = {
        let label = UILabel(fontSize: 15, fontColor: UIColor.darkGrayColor())
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = width - 2*margin
        return label
    }()
    
    // 图片
    lazy var pictureView:StatusPictureView? = StatusPictureView()
    // 底部的View
    lazy var bottomView:StatusBottomView = StatusBottomView()
    
   
}
